 /*
  * Copyright (c) 2021, 2023, yishun.cn All rights reserved.
  *
  */
 package cn.shun.config;

 import com.alibaba.druid.pool.DruidDataSource;
 import com.github.pagehelper.PageInterceptor;
 import org.mybatis.spring.SqlSessionFactoryBean;
 import org.mybatis.spring.annotation.MapperScan;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.ComponentScan;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.context.annotation.PropertySource;

 /**
  * <p>Project: MyBatis2023 - MybatisConfig
  * <p>Powered by yishun On 2023-01-19 14:59:17
  *
  * @author yishun [2465734101@qq.com]
  * @version 1.0
  * @since 17
  */
 @Configuration
 @PropertySource("classpath:db.properties")
 //扫描有@Mapper注解的接口，主要解决单元测试报红
 @ComponentScan("cn.shun.mapper")
 //扫描包，在接口上可以不加@Mapper
 @MapperScan("cn.shun.mapper")

 public class MybatisConfig {
     @Value("${db.driver}")
     private String driver;
     @Value("${db.url}")
     private String url;
     @Value("${db.username}")
     private String username;
     @Value("${db.password}")
     private String password;


     @Bean(name = "ds")
     public DruidDataSource ds() {
         DruidDataSource ds = new DruidDataSource();
         ds.setDriverClassName(driver);
         ds.setUrl(url);
         ds.setUsername(username);
         ds.setPassword(password);
         return ds;
     }

     @Bean(name = "sf")
     public SqlSessionFactoryBean sf(DruidDataSource ds) throws Exception {
         SqlSessionFactoryBean sf = new SqlSessionFactoryBean();
         sf.setDataSource(ds);
         //添加分页插件拦截器
         sf.setPlugins(new PageInterceptor());
         sf.setTypeAliasesPackage("cn.shun.entity");
         return sf;
     }


 }
